using Microsoft.AspNetCore.Mvc;
using PdfiumViewer.Core;
using System.Drawing.Imaging;

namespace PDFiumViewer.demo_web.Controllers
{
    public class homeController : Controller
    {
        public IActionResult index()
        {
            return View();
        }

        public IActionResult test()
        {
            string inFilePath = Path.Combine("wwwroot", "uploads", "test.pdf");
            using (var document = PdfDocument.Load(inFilePath))
            {
                var pageCount = document.PageCount;
                for (int i = 0; i < pageCount; i++)
                {
                    var dpi = 300;
                    using (var image = document.Render(i, dpi, dpi, PdfRenderFlags.CorrectFromDpi))
                    {
                        var encoder = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid);
                        var encParams = new EncoderParameters(1);
                        encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 10L);
                        string outFilePath = Path.Combine("wwwroot", "uploads", $"test_{i + 1}.jpg");
                        image.Save(outFilePath, encoder, encParams);
                    }
                }
            }
            return Ok("success");
        }
    }
}
